package hongniang

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
	"shangyuapi/service"
	"time"
)

type OrderslistBody struct {
	Hongniangid int64 `json:"hongniangid" gorm:"hongniangid"`
	PageNo      int   `json:"pageNo"`
	Pagesize    int   `json:"pagesize"`
	Cityid      int64 `json:"cityid"`
	Classify    int64 `json:"classify"`
}

type OrderslistRBody struct {
	Data       []models.Orders `json:"data"`
	PageNo     int             `json:"pageNo"`
	PageSize   int             `json:"pageSize"`
	TotalCount int64           `json:"totalCount"`
	TotalPage  int64           `json:"totalPage"`
}

func Orderslist(ctx *gin.Context) {
	request := OrderslistBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	classify := request.Classify
	//cityid := request.Cityid

	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var orders []models.Orders
	res := data_source.Db
	if classify == 1 {
		res = res.Where("hongniangid = 0")
		//if cityid > 0{
		//	res = res.Where("")
		//}
	} else if classify == 2 {
		res = res.Where("hongniangid = ?", hongniangid).Where("status = 1").Where("fuwu = ? or fuwu = ? or fuwu = ?", "到店咨询", "", "定制化脱单")
	} else if classify == 3 {
		res = res.Where("hongniangid = ?", hongniangid).Where("status = 1 or status = 3 or status = 4")
	} else if classify == 4 {
		res = res.Where("hongniangid = ?", hongniangid).Where("status = 7 or status = 8")
	} else if classify == 5 {
		res = res.Where("hongniangid = ?", hongniangid).Where("status = 2 or status = 6")
	}
	res.Model(&models.Orders{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&orders)

	var uidarr []int64
	for _, v := range orders {
		uidarr = append(uidarr, v.Uid)
		uidarr = append(uidarr, v.Userid)
	}
	var users []models.Userdingdan
	data_source.Db.Model(&models.User{}).Where("id in (?)", uidarr).Find(&users)

	var miyao string
	miyao = service.Miyao()
	key := []byte(miyao)

	for k, o := range orders {
		for _, u := range users {
			if o.Uid == u.ID {
				phonejm, _ := service.Encrypt(key, u.Phone)
				u.Phone = phonejm
				orders[k].U = u
			}
			if o.Userid == u.ID {
				phonejm, _ := service.Encrypt(key, u.Phone)
				u.Phone = phonejm
				orders[k].User = u
			}
		}
	}

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := OrderslistRBody{
		Data:       orders,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type OrderBody struct {
	ID          int64  `json:"id" gorm:"id"`
	Uid         int64  `json:"uid" gorm:"uid"`       //生成订单人
	Userid      int64  `json:"userid" gorm:"userid"` //对方id
	Hongniangid int64  `json:"hongniangid" gorm:"hongniangid"`
	Yhtime      int64  `json:"yhtime" gorm:"yhtime"`
	Yharea      string `json:"yharea" gorm:"yharea"`
	Hntitle     string `json:"hntitle" gorm:"hntitle"`
	Hnimages    string `json:"hnimages" gorm:"hnimages"`
	Shenhe      int64  `json:"shenhe" gorm:"shenhe"`
}

func Orderdel(ctx *gin.Context) {
	request := OrderBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var order models.Orders
	data_source.Db.Where("id = ?", id).First(&order)

	data_source.Db.Model(&models.Orders{}).Where("id = ?", id).Update("status", 6)
	data_source.Db.Model(&models.Orders{}).Where("id = ?", id).Update("upriceuid", 0)
	data_source.Db.Model(&models.Orders{}).Where("id = ?", id).Update("userpriceuid", 0)

	var user models.User
	data_source.Db.Where("id = ?", order.Uid).First(&user)
	data_source.Db.Model(&models.User{}).Where("id = ?", order.Uid).Update("xingbi", user.Xingbi+order.Uprice)

	var user1 models.User
	data_source.Db.Where("id = ?", order.Userid).First(&user1)
	data_source.Db.Model(&models.User{}).Where("id = ?", order.Userid).Update("xingbi", user1.Xingbi+order.Userprice)
	var xingbis []models.Userxingbi

	xingbi := models.Userxingbi{
		Uid:     order.Uid,
		Price:   order.Uprice,
		Status:  1,
		Title:   "约会付款退回",
		Addtime: time.Now().Unix(),
		Dqprice: user.Xingbi + order.Uprice,
	}
	xingbis = append(xingbis, xingbi)

	xingbi1 := models.Userxingbi{
		Uid:     order.Userid,
		Price:   order.Userprice,
		Status:  1,
		Title:   "约会付款退回",
		Addtime: time.Now().Unix(),
		Dqprice: user.Xingbi + order.Userprice,
	}
	xingbis = append(xingbis, xingbi1)
	data_source.Db.Create(&xingbis)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Ordersshenhe(ctx *gin.Context) {
	request := OrderBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	shenhe := request.Shenhe

	var order models.Orders
	data_source.Db.Where("id = ?", id).First(&order)

	if shenhe == 2 {
		orderadd := models.Orders{
			Status: shenhe,
		}
		data_source.Db.Where("id = ?", id).Updates(&orderadd)
		data_source.Db.Model(&models.Orders{}).Where("id = ?", id).Update("upriceuid", 0)

		var user models.User
		data_source.Db.Where("id = ?", order.Uid).First(&user)
		data_source.Db.Model(&models.User{}).Where("id = ?", order.Uid).Update("xingbi", user.Xingbi+order.Uprice)
		xingbi := models.Userxingbi{
			Uid:     order.Uid,
			Price:   order.Uprice,
			Status:  1,
			Title:   "约会付款退回",
			Addtime: time.Now().Unix(),
			Dqprice: user.Xingbi + order.Uprice,
		}
		data_source.Db.Create(&xingbi)

	} else if shenhe == 3 {
		orderadd := models.Orders{
			Status: shenhe,
		}
		data_source.Db.Where("id = ?", id).Updates(&orderadd)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Orderwc(ctx *gin.Context) {
	request := OrderBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	hntitle := request.Hntitle
	hnimages := request.Hnimages

	orderadd := models.Orders{
		Hnimages: hnimages,
		Hntitle:  hntitle,
		Status:   7,
		Wctime:   time.Now().Unix(),
	}
	data_source.Db.Where("id = ?", id).Updates(&orderadd)

	var order models.Orders
	data_source.Db.Where("id = ?", id).First(&order)

	var hongning models.Hongniang
	data_source.Db.Where("id = ?", order.Hongniangid).First(&hongning)

	hnpirce := models.Hongniangprice{
		Hongniangid: order.Hongniangid,
		State:       1,
		Addtime:     time.Now().Unix(),
		Title:       "完成订单",
		Price:       order.Fwprice,
		Houprice:    hongning.Price + order.Fwprice,
	}
	data_source.Db.Create(&hnpirce)
	hnupdate := models.Hongniang{
		Price:    hongning.Price + order.Fwprice,
		Allprice: hongning.Allprice + order.Fwprice,
	}
	data_source.Db.Where("id = ?", order.Hongniangid).Updates(&hnupdate)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Orderarea(ctx *gin.Context) {
	request := OrderBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	yhtime := request.Yhtime
	yharea := request.Yharea

	orderadd := models.Orders{
		Yharea: yharea,
		Yhtime: yhtime,
	}
	data_source.Db.Where("id = ?", id).Updates(&orderadd)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Ordersadd(ctx *gin.Context) {
	request := OrderBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	uid := request.Uid
	userid := request.Userid
	yhtime := request.Yhtime
	yharea := request.Yharea

	var fuwu models.Fuwu
	data_source.Db.Where("id = 2").First(&fuwu)

	orderadd := models.Orders{
		Uid:         uid,
		Userid:      userid,
		Hongniangid: hongniangid,
		Status:      3,
		Addtime:     time.Now().UnixNano(),
		Uprice:      fuwu.Price,
		Userprice:   fuwu.Price,
		Fwprice:     fuwu.Price + fuwu.Price,
		Price:       fuwu.Price + fuwu.Price,
		Yhprice:     0,
		State:       2,
		Fuwu:        "1v1约会",
		Yharea:      yharea,
		Yhtime:      yhtime,
	}
	data_source.Db.Create(&orderadd)

	var orderusers []models.Orderuser
	orderuseradd := models.Orderuser{
		Uid:     uid,
		Orderid: orderadd.ID,
	}
	orderusers = append(orderusers, orderuseradd)
	orderuseradd1 := models.Orderuser{
		Uid:     userid,
		Orderid: orderadd.ID,
	}
	orderusers = append(orderusers, orderuseradd1)
	data_source.Db.Create(&orderusers)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Ordersjd(ctx *gin.Context) {
	request := OrderBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	id := request.ID

	orderadd := models.Orders{
		Hongniangid: hongniangid,
		Status:      3,
	}
	data_source.Db.Where("id = ?", id).Updates(&orderadd)

	var order models.Orders
	data_source.Db.Where("id = ?", id).First(&order)
	orderuseradd := models.Orderuser{
		Uid:     order.Userid,
		Orderid: id,
	}
	data_source.Db.Create(&orderuseradd)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
